import asyncio, pyautogui, random, os, time

from core.base_controller import BaseController
from core.utils import focus_wow, get_image_pos

class ResetWowExeController(BaseController):

    async def kill_wow_process(self):
        await self.log("kill wow process start")
        os.system('taskkill /f /im Wow.exe')
        # os.system('taskkill /f /im Battle.net.exe')
        # os.system('taskkill /f /im Agent.exe')
        await asyncio.sleep(10)
        await self.log("kill wow process end")
        
    async def start_wow_process(self, account):
        relink_btn_pos = get_image_pos("images_2/relink_btn.png", confidence = 0.8)
        if relink_btn_pos:
            pyautogui.click(relink_btn_pos)
        
        await asyncio.sleep(25)
        # await self.log("start Battle.net Launcher.exe")
        # os.startfile(r"C:\Program Files (x86)\Battle.net\Battle.net Launcher.exe")
        # await asyncio.sleep(20)
        # await self.log("start Battle.net Launcher.exe end")
        await self.log("start Wow.exe start")
        setting_btn_pos = None
        while True:
            setting_btn_pos = get_image_pos("images_2/agent_setting_btn.png", confidence = 0.8)
            if setting_btn_pos:
                break
            await asyncio.sleep(5)
        enter_btn_pos = None
        while True:
            enter_btn_pos = get_image_pos("images_2/agent_enter_btn.png", confidence = 0.8)
            if enter_btn_pos:
                break
            pyautogui.click(x=setting_btn_pos.x - 100, y=setting_btn_pos.y)
            await asyncio.sleep(5)
        
        # 切换子账号    
        pyautogui.click(x=setting_btn_pos.x, y=setting_btn_pos.y-50)
        await asyncio.sleep(2)
        account_pos_y_offset = 320
        if account == "wow3":
            account_pos_y_offset = 250
        await self.log("account = {},offset={}".format(account, account_pos_y_offset))
        pyautogui.click(x=setting_btn_pos.x-200, y=setting_btn_pos.y-account_pos_y_offset)
        await asyncio.sleep(2)
        
        pyautogui.click(enter_btn_pos)
        await asyncio.sleep(30)
        server_select_pos = get_image_pos("images_2/server_select.png", confidence=0.8)
        if server_select_pos:
            pyautogui.doubleClick(x=server_select_pos.x, y=server_select_pos.y+42)
            await asyncio.sleep(20)

        search_bar_pos = None
        while True:
            search_bar_pos = get_image_pos("images_2/search_bar_image.png", confidence=0.6)
            if search_bar_pos:
                break
            await asyncio.sleep(5) 
        await self.log("start Wow.exe end")

        
        
    async def loop_cmds(self, account):
        await self.kill_wow_process()
        await self.start_wow_process(account)

    async def start(self, extra_data = {}):
        await self.stop_all_task()  # 停止之前所有任务
        await self.log("ResetWowExeController Start")
        account = extra_data.get("account", "wow1")
        task = asyncio.create_task(self.loop_cmds(account))  # 创建任务
        self.add_task(task)  # 添加任务到列表
            

    async def stop(self):
        """ 停止定时器 """
        await self.stop_all_task()  # 停止所有任务